package com.example.kaoqinmanager.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.dylanc.viewbinding.base.ViewBindingUtil;
import com.example.kaoqinmanager.activity.LoginActivity;
import com.example.kaoqinmanager.activity.MainActivity;
import com.example.kaoqinmanager.utils.SharePreferenceUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by 大草原水 on 2021/12/11
 */

public abstract class BaseBindingActivity<VB extends ViewBinding> extends AppCompatActivity {
    private static List<BaseBindingActivity> activitys = new LinkedList<BaseBindingActivity>();
    public static String TAG;
    private VB binding;
    public Activity mActivity;
    public SharePreferenceUtil mSharePreferenceUtil;
    public ActionBar actionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ViewBindingUtil.inflateWithGeneric(this, getLayoutInflater());
        setContentView(binding.getRoot());
        TAG = this.getClass().getSimpleName();
        mActivity = this;
        activitys.add(this);
        mSharePreferenceUtil = SharePreferenceUtil.getInstance(this);
        actionBar = getSupportActionBar();
    }

    public VB getBinding() {
        return binding;
    }

    private long mPreClickTime;

    @Override
    public void onBackPressed() {
        if (this instanceof MainActivity || this instanceof LoginActivity) {// 主页
            if (System.currentTimeMillis() - mPreClickTime > 2000) {// 两次点击的间隔大于2s中
                Toast.makeText(getApplicationContext(), "再按一次退出", Toast.LENGTH_SHORT).show();
                mPreClickTime = System.currentTimeMillis();
                return;
            } else {
                // 完全退出
                exit();
            }
        } else {
            super.onBackPressed();// finish
        }
    }

    /**
     * 完全退出
     */
    public void exit() {
        super.onBackPressed();
        for (BaseBindingActivity activity : activitys) {
            activity.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activitys.remove(this);
    }

    public void openActivity(Class<? extends AppCompatActivity> clazz) {
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        startActivity(intent);
        //overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
    }
}